<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Profile extends CI_Controller {
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$data['title'] = "New Paltz HvZ - ".$this->session->userdata('username')."";
		$this->load->helper('form');
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('profile_view');
		$this->load->view('includes/footer_view');
	}
	
	public function signin()
	{
		$data['title'] = "New Paltz HvZ - Sign In";
		$this->load->helper('form');
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('signin_view');
		$this->load->view('includes/footer_view');
	}

	public function settings(){
		if ($this->session->userdata('username') !== FALSE && $this->session->userdata('is_logged_in') !== FALSE){
		$data['title'] = "New Paltz HvZ - Settings";
		$this->load->helper('form');
		$this->load->view('includes/header_view',$data);
		$this->load->view('includes/navbar_view');
		$this->load->view('settings_view');
		$this->load->view('includes/footer_view');
	}
		else {
		//redirect to error page.
		redirect("home/");	
	} 
	}
	
	
	function submit_status(){
		$this->load->library('form_validation');
		$this->load->model('User_model');
		
		$this->form_validation->set_rules('status','Status','trim|required|min_length[1]|xss_clean');
		
		if($this->form_validation->run() == FALSE){
			$data['title'] = "New Paltz HvZ - Sign In";
			$this->load->helper('form');
			$this->load->view('signin_view.php', $data);
		}
		else{
			$status = $this->input->post('status');
			$user = $this->input->post('user');
			$user_id = $this->input->post('User_id');
			
			$query = $this->User_model->insert_status($status,$user,$user_id);
			
			redirect(base_url()."profile/");
		}
	}

	function insert_comment(){
		$this->load->library('form_validation');
		$this->load->model('User_model');
		
		$this->form_validation->set_rules('comment','Comment','trim|required|xss_clean');
		$this->form_validation->set_rules('status_id','Status ID','required|is_natural|xss_clean');
		$this->form_validation->set_rules('User_id','User_id','required|xss_clean');
		
		if($this->form_validation->run() == FALSE){
			$this->index();
		}
		else{
			$comment = $this->input->post('comment');
			$status_id = $this->input->post('status_id');
			$User_id = $this->input->post('User_id');
			
			$query = $this->User_model->insert_comment($comment,$status_id,$User_id);
			
			if($query)
			{
				die('success');
			}
			
			redirect(base_url()."profile/");
		}
	}
	
	function upload_profile_image(){
		$this->laod->library("form_validation");
		
		$this->form_validation->set_rules('username',"Username",'required|xss_clean');
		$this->form_validation->set_rules('userfile','Image','required|xss_clean');
		$this->form_validation->set_rules('User_id','User_id','required|xss_clean');
		
		if($this->form_validation->run() == FALSE){
			$this->settings();
		}
		else {
			$username = $this->input->post("username");
			$User_id = $this->input->post("User_id");
			//$main_path = "http://localhost/hvz/profile_images/";
			$thumb_path = $main_path."thumbs/";
			
			//some $config vars for image
	        $config['upload_path'] = 'http://localhost/hvz/profile_images/';
	        $config['allowed_types'] = 'jpg|jpeg|png';
	        $config['max_size'] = '6048';
	        $config['remove_spaces'] = true;
            $config['overwrite'] = true;
            $config['max_width'] = '1000';
            $config['max_height'] = '1000';
			$config['file_name'] = $username; 
            $this->load->library('upload', $config);	
            //upload main image
            if(!$this->upload->do_upload('photo')){
                $e = $this->upload->display_errors();
                print_r($e);
            }
            $image = $this->upload->data();
			
			


    //thumbnail creation start
            $config1['image_library'] = 'gd2';
            $config1['source_image'] = $image['full_path'];
			$config1['upload_path'] = "http://localhost/hvz/profile_images/thumbs/";
            $config1['create_thumb'] = TRUE;
            $config1['maintain_ratio'] = TRUE;
            $config1['width'] = 50;
            $config1['height'] = 50;

            $this->load->library('image_lib', $config1);
            $this->image_lib->resize();
            //thumbnail creation ends
            
            $image_name = $image['file_name'];
			
            

				
				$image_name = $image_base_name.".png";
				$thumb_name = $image_thumb_name.".png";
				
				$data = array(
					"image" => $image_name,
					"image_thumb" => $thumb_name
				);
				
				$this->db->where('id',$User_id);
				$this->db->update("Users",$data);
		}
	}
	
	
	function logout(){
		
		$this->session->sess_destroy();
		redirect("http://localhost/hvz/index.php/home/");
	}
	
	//Helper functions.
	
	function _randomString($length){
		$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		$string ="";
		for($i=0;$i<$length;$i++)
			$string .= $characters[rand(0, strlen($characters) - 1)];
		return $string;
	}
	
	
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */